package control;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import control.connector.Taxa;
import control.connector.TaxaObserver;

public class TaxaFactory {

	private static List<Taxa> taxis = new ArrayList<Taxa>();
	private static Set<TaxaObserver> observers = new HashSet<TaxaObserver>();

	public static Taxa getTaxa(int taxaid) {
		for (Taxa taxa : taxis) {
			if (taxa.getTaxaid() == taxaid) {
				return taxa;
			}
		}
		Taxa taxa = new Taxa(taxaid);
		taxis.add(taxa);
		for (TaxaObserver observer : observers) {
			taxa.addObserver(observer);
		}
		return taxa;
	}

	public static List<Taxa> getAllTaxis() {
		return Collections.unmodifiableList(taxis);
	}

	public static void addObserver(TaxaObserver observer) {
		observers.add(observer);
		updateObservers();
	}

	private static void updateObservers() {
		for (Taxa taxa : taxis) {
			for (TaxaObserver observer : observers) {
				taxa.addObserver(observer);
			}
		}
	}

}
